﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TED.ViewModels;
using Microsoft.Phone.Controls;
using TED.Model;

namespace TED.View
{
    public partial class Favourite
    {
        private FavouriteVideoModel fvm;
        private VideoViewModel vvm;

        public Favourite()
        {
            InitializeComponent();
            fvm = new FavouriteVideoModel();
            vvm = new VideoViewModel();
            this.FavVideosListBox.DataContext = fvm.FavouriteVideoList;
            this.Loaded += new RoutedEventHandler(Favourite_Loaded);        
        }

        private void Favourite_Loaded(object sender, RoutedEventArgs e)
        {
           
        }

        private void ViewFavoriteVideos_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/Favourite.xaml", UriKind.Relative));
        }

        private void ViewSearchPage_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/SearchPage.xaml", UriKind.Relative));
        }

        private void PlayVideoFromPlayList_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Video.xaml", UriKind.Relative));
        }

        private void ViewMainPage_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void RemoveFavourite_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem selectedListBoxItem = this.FavVideosListBox.ItemContainerGenerator.ContainerFromItem
                ((sender as MenuItem).DataContext) as ListBoxItem;

            if (selectedListBoxItem == null)
            {
                return;
            }
            string viewUrl = ((TEDVideo)selectedListBoxItem.Content).ViewUrl.ToString();
            
            fvm.RemoveVideoFromFavourite(viewUrl);

            // Update UI by setting ItemsSource to null, and then apply a new ItemsSource
            // This seems to be a "dumb" way to do things, but I don't know how to update the UI another way
            this.FavVideosListBox.ItemsSource = null;
            this.FavVideosListBox.ItemsSource = fvm.FavouriteVideoList;
        }

        private void AddVideoToPlayList_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem selectedListBoxItem = this.FavVideosListBox.ItemContainerGenerator.ContainerFromItem
               ((sender as MenuItem).DataContext) as ListBoxItem;

            if (selectedListBoxItem == null)
            {
                return;
            }
            string author = ((TEDVideo)selectedListBoxItem.Content).Author.ToString();
            string viewUrl = ((TEDVideo)selectedListBoxItem.Content).ViewUrl.ToString();
            Uri imageUrl = new Uri(((TEDVideo)selectedListBoxItem.Content).ImageUrl.ToString());
            string title = ((TEDVideo)selectedListBoxItem.Content).Title.ToString();
            string videoUrl;
            if (((TEDVideo)selectedListBoxItem.Content).VideoUrl != null)
            {
                videoUrl = ((TEDVideo)selectedListBoxItem.Content).VideoUrl.ToString();
            }
            else
                videoUrl = null;

            //TEDVideo t = new TEDVideo(title, author, imageUrl, viewUrl, videoUrl);
            TEDVideo t = (new TEDVideo
            {
                Title = title,
                Author = author,
                ViewUrl = viewUrl,
                ImageUrl = imageUrl,
                VideoUrl = videoUrl,
            });
            vvm.AddVideoToPlaylist(t);
        }

        private void GestureDoubleTap_PlayVideo(object sender, GestureEventArgs e)
        {
            ListBoxItem selectedListBoxItem = this.FavVideosListBox.ItemContainerGenerator.ContainerFromItem
                ((sender as Grid).DataContext) as ListBoxItem;

            if (selectedListBoxItem == null)
            {
                return;
            }
            string author = ((TEDVideo)selectedListBoxItem.Content).Author.ToString();
            string viewUrl = ((TEDVideo)selectedListBoxItem.Content).ViewUrl.ToString();
            Uri imageUrl = new Uri(((TEDVideo)selectedListBoxItem.Content).ImageUrl.ToString());
            string title = ((TEDVideo)selectedListBoxItem.Content).Title.ToString();
            string videoUrl = null;
            //TEDVideo t = new TEDVideo(title, author, imageUrl, viewUrl, videoUrl);
            TEDVideo t = (new TEDVideo
            {
                Title = title,
                Author = author,
                ViewUrl = viewUrl,
                ImageUrl = imageUrl,
                VideoUrl = videoUrl,
            });

            vvm.AddVideoToPlaylist(t);

            (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Video.xaml", UriKind.Relative));
            //Video v = new Video(t);


            MessageBox.Show("You double tap it");

        }
    }
}
